package com.df.barcodescaning.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;

import com.df.barcodescaning.R;
import com.df.barcodescaning.bean.UserData;
import com.df.barcodescaning.bean.UserGroup;
import com.df.barcodescaning.widget.ClearEditText;

import java.util.Map;

import static com.df.barcodescaning.R.id.username;

public class AddUserActivity extends AppCompatActivity implements View.OnClickListener {

    private ClearEditText userNameView;
    private ClearEditText passwordView;
    private ClearEditText repasswordView;
    private TextInputLayout userNameTextInputLayout;
    private TextInputLayout passwordTextInputLayout;
    private TextInputLayout repasswordTextInputLayout;

    private UserGroup userGroup;
    private Map<String, UserData> userMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_user);

        userGroup = (UserGroup) getIntent().getSerializableExtra(UserManageActivity.KEY_SERIAL_USER_GROUP);
        userMap = userGroup.getValues();

        findViewById(R.id.btn_register).setOnClickListener(this);
        findViewById(R.id.btn_exit).setOnClickListener(this);
        userNameTextInputLayout= (TextInputLayout) findViewById(username);
        userNameView = (ClearEditText)userNameTextInputLayout.getEditText();
        passwordTextInputLayout = (TextInputLayout) findViewById(R.id.password);
        passwordView = (ClearEditText) passwordTextInputLayout.getEditText();
        repasswordTextInputLayout = (TextInputLayout) findViewById(R.id.confirm_psw);
        repasswordView = (ClearEditText) repasswordTextInputLayout.getEditText();
    }

    private void registerUser() {

        userNameTextInputLayout.setError(" ");
        passwordTextInputLayout.setError(" ");
        repasswordTextInputLayout.setError(" ");
        final String username = userNameView.getText().toString().trim();
        if(TextUtils.isEmpty(username)){    //未输入用户名
            userNameTextInputLayout.setError(getString(R.string.msg_login_username_null));
            return;
        }
        final String pwd= passwordView.getText().toString().trim();
        if(TextUtils.isEmpty(pwd)){ //未输入密码
            passwordTextInputLayout.setError(getString(R.string.msg_login_passwork_null));
            return;
        }
        final String repwd= repasswordView.getText().toString().trim();
        if(TextUtils.isEmpty(repwd)){   //未输入验证密码
            repasswordTextInputLayout.setError(getString(R.string.msg_login_passwork_null));
            return;
        }
        if(!TextUtils.equals(pwd, repwd)) { //两次输入密码不同
            repasswordTextInputLayout.setError(getString(R.string.msg_register_nosame_passwork));
            return;
        }
        if (userMap.containsKey(username)) {    //用户已经注册
            userNameTextInputLayout.setError(getString(R.string.msg_register_exist));
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("username", userNameView.getText().toString().trim());
        intent.putExtra("password", passwordView.getText().toString().trim());
        setResult(UserManageActivity.CODE_REQUEST_ADD, intent);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register:
                registerUser();
                break;
            case R.id.btn_exit:
                finish();
                break;
        }
    }
}
